// 本例与Promise无关，仅说明Promise是如何调用executor函数的

class Arithmetic {
    constructor(executor){

        function addition(...params){
            //let sum = 0;
            //params.forEach( n => sum += n );
            //return sum;
            return params.reduce( (prev,curr) => prev + curr , 0);
        }

        function subtraction(a, b) {
            return a - b ;
        }

        function multiplication(...params){
            if(params.indexOf(0) != -1) {
                return 0;
            }
            return params.reduce( (prev,curr) => prev * curr , 1);
        }

        function division(a,b) {
            if( b == 0 ) {
                throw new Error('除数不能为零');
            }
            return a / b ;
        }

        // 调用通过参数传入的函数
        executor( addition, subtraction, multiplication, division );
    }
}

let executor = (add, sub, mult, div) => {
    let sum = add(1,2,3,4,5);
    console.log( sum );

    let subtract = sub(900, 600);
    console.log( subtract );

    let m = mult(1,2,3,4,5,6,7);
    console.log( m );

    let d = div(100, 3);
    console.log( d );
}

let a = new Arithmetic( executor );